<?php

namespace app\admin\controller;

use app\admin\service\OrderService;
use support\spark\model\goods\Goods;
use support\spark\model\order\Order;
use support\spark\model\order\OrderDetail;
use support\spark\model\order\OrderStatusChange;
use support\spark\model\system\SetExpress;
use support\Db;
use support\Request;
use support\View;

class OrderController extends Curd
{
    /**
     * 订单列表
     */
    public function index()
    {
        if (request()->isAjax()) {
            $param = request()->all();
            $orderService = new OrderService();
            $orderList = $orderService->getList($param);
            return sparkSuccess($orderList);
        }
        return view('order/index');
    }

    /**
     * 发货
     */
    public function express()
    {
        if (request()->isAjaxPost()) {

            $param = request()->post();

            $orderService = new OrderService();
            $res = $orderService->doExpress($param);
            return sparkSuccess($res);
        }

        $orderId = request()->input('id');
        $setExpressModel = new SetExpress();
        $expressList = $setExpressModel->getAllList([
            'status' => 1
        ], ['name', 'code'], ['id' => 'asc']);

        $orderDetail = new OrderDetail();
        $orderInfo = $orderDetail->getInfoByWhere([
            'order_id' => $orderId
        ], ['goods_id']);

        $goodsModel = new Goods();
        $goodsInfo = $goodsModel->getInfoByWhere([
            'id' => $orderInfo['goods_id']
        ], ['type']);

        return sparkSuccess(dataReturn(0, 'success', [
            'express' => $expressList,
            'order_id' => $orderId,
            'goods_type' => $goodsInfo['type']
        ]));
    }

    /**
     * 展示物流信息
     */
    public function showExpress()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $orderService = new OrderService();
            $res = $orderService->showExpress($param);
            return sparkSuccess($res);
        }
        $orderId = request()->input('id');
        $setExpressModel = new SetExpress();
        $expressList = $setExpressModel->getAllList([
            'status' => 1
        ], ['name', 'code'], ['id' => 'asc']);

        $orderModel = new Order();
        $info = $orderModel->getInfoByWhere([
            'id' => $orderId
        ]);

        View::assign([
            'express' => $expressList,
            'order_id' => $orderId,
            'info' => $info
        ]);

        return view('order/add');
    }

    /**
     * 订单详情
     */
    public function detail()
    {
        $orderId = request()->input('id');
        $orderService = new OrderService();
        $res = $orderService->showDetail($orderId);
        return sparkSuccess($res);
    }

    /**
     * 支付日志
     */
    public function log()
    {
        $orderId = request()->input('id');

        $orderStatusModel = new OrderStatusChange();
        $list = $orderStatusModel->getAllList([
            'order_id' => $orderId
        ]);
        return sparkSuccess(dataReturn(0, 'ok', $list));

    }

    /**
     * 删除订单
     */
    public function del()
    {
        $orderId = request()->input('id');
        $orderModel = new Order();
        $orderModel->where('id', $orderId)->update([
            'is_del' => 2
        ]);
        return sparkSuccess(dataReturn(0, '删除成功'));
    }

    /**
     * 统计订单
     */
    public function census(Request $request)
    {
        $orderParam = [
            0 => 0,
            1 => 0,
            2 => 0,
            3 => 0,
            4 => 0,
            5 => 0
        ];

        $list = Db::name('order')->fieldRaw('type,count(*) as `total`')->where('pid', '=', 0)->where('status', 3)
            ->where('is_del', 1)->group('type')->select();

        if (!empty($list)) {
            foreach ($list as $vo) {
                $orderParam[$vo['type']] = $vo['total'];
            }
        }

        $orderParam[0] = Db::name('order')->where('pid', '=', 0)->where('status', 3)->where('is_del', 1)->count();
        $orderParam[5] = Db::name('order')->where('is_del', 2)->count();
        return sparkSuccess(dataReturn(0, 'success', $orderParam));
    }

    /**
     * 导出订单
     */
    public function export(Request $request)
    {
        $param = request()->all();

        $orderService = new OrderService();
        $res = $orderService->dealExport($param);
        return sparkSuccess( dataReturn(0, 'success', $res['data']));
    }

    /**
     * 完成订单
     */
    public function complete(Request $request)
    {
        $orderId = request()->input('id');
        $orderService = new OrderService();
        $res = $orderService->completeOrder($orderId);
        return  sparkSuccess(dataReturn($res));
    }
}
